<template>
	<view class="personal-box">
		<u-form labelPosition="top" :model="form" ref="form">
			<u-form-item prop="account" label="扫描条形码">
				<u-image @click="scanBarcode" src="../../static/icon/enterprise.png" mode="widthFix"
					class="idcar"></u-image>
			</u-form-item>
			<u-form-item prop="name" label="药品名称">
				<u-input v-model="form.name" placeholder="请输入药品名称" :placeholder-style="placeholderStyle"></u-input>
			</u-form-item>
			<u-form-item prop="specification" label="规格">
				<u-input v-model="form.specification" placeholder="请输入规格" :placeholder-style="placeholderStyle" />
			</u-form-item>
			<u-form-item prop="dosage" label="剂量">
				<u-input v-model="form.dosage" placeholder="请输入剂量" :placeholder-style="placeholderStyle" />
			</u-form-item>
			<u-form-item prop="reverso" label="功能主治">
				<u-input v-model="form.reverso" placeholder="请输入功能主治" :placeholder-style="placeholderStyle" />
			</u-form-item>
			<u-form-item prop="manufacturer" label="生产商">
				<u-input v-model="form.manufacturer" placeholder="请输入生产商" :placeholder-style="placeholderStyle" />
			</u-form-item>
			<u-form-item prop="expirationDate" label="有效期">
				<u-calendar max-date="2999-12-31" v-model="show" :mode="mode" @change="change"></u-calendar>
				<view @click="show=!show">{{form.expirationDate || '选择时间'}}</view>
				<!-- <u-input v-model="form.expirationDate" placeholder="请输入有效期" :placeholder-style="placeholderStyle" /> -->
			</u-form-item>
			<u-form-item prop="stock" label="剩余库存">
				<u-input v-model="form.stock" type='number' placeholder="请输入剩余库存" :placeholder-style="placeholderStyle" />
			</u-form-item>
			<u-form-item prop="approvalNumber" label="批准文号">
				<u-input v-model="form.approvalNumber" placeholder="请输入批准文号" :placeholder-style="placeholderStyle" />
			</u-form-item>
			<u-form-item prop="description" label="描述">
				<rich-text style="font-size: 18rpx;" :nodes="form.description"></rich-text>
			</u-form-item>
		</u-form>
		<view class="card_box card_text_login" @click="submit()">
			提交
		</view>
	</view>
</template>

<script>
	import {
		findByPage
	} from '@/api/datasource'
	import * as medicine from '@/api/medicine'
	export default {
		data() {
			return {
				placeholderStyle: 'color:#aaaaaa',
				form: {
					id: 0,
					name: '',
					specification: '',
					dosage: '',
					manufacturer: '',
					expirationDate: '',
					stock: '',
					approvalNumber: '',
					description: '',
					reverso:''
				},
				mode: 'date',
				show: false,
				list: []
			}
		},
		onLoad(options) {
			this.form.id = options.id || 0
			if (this.form.id) {
				this.init()
			}
		},
		methods: {
			change(e) {
				this.form.expirationDate = e.result
			},
			init() {
				medicine.info(this.form.id).then(res => {
					this.form = res.data
				})

			},
			submit(){
				this.$u.debounce(this.submitdata, 500)
			},
			submitdata() {
				if(!this.form.expirationDate) return uni.showToast({
							icon: 'error',
							title: '有效期未设置',
							duration: 2000
						});
						if(!this.form.stock) return uni.showToast({
									icon: 'error',
									title: '库存未设置',
									duration: 2000
								});
				if (this.form.id) {
					medicine.update(this.form).then(res => {
						uni.showToast({
							icon: 'none',
							title: '保存成功',
							duration: 2000
						});
						this.reLaunch('/pages/index/index')
					})
				} else {
					medicine.add(this.form).then(res => {
						uni.showToast({
							icon: 'success',
							title: '保存成功',
							duration: 2000
						});
						this.reLaunch('/pages/index/index')
						
					})
				}


			},
			scanBarcode() {
				uni.scanCode({
					success: (res) => {
						findByPage({
							page: 1,
							size: 10,
							barcode: res.result
						}).then(resdata => {
							if (resdata.data[resdata.data.length -1]) {
								this.form.name = resdata.data[0][0].name
								this.form.specification = resdata.data[0][0].spec
								this.form.dosage = resdata.data[0][0].drug_dose
								this.form.reverso = resdata.data[0][0].indication
								this.form.manufacturer = resdata.data[0][0].drug_from
								this.form.approvalNumber =resdata.data[0][0].drug_pzwh
								this.form.description = resdata.data[0][0].drug_instruction
							} else {
								this.form = {
									name: '',
									specification: '',
									dosage: '',
									manufacturer: '',
									expirationDate: '',
									stock: '',
									approvalNumber: '',
									description: '',
									reverso: ''
								}
								this.showToast('未查询到此药品')
							}

						})

					},
					fail: (err) => {
						// 扫描失败回调
						console.error('扫描失败：' + err);
					}
				});
			}
		}
	}
</script>

<style>
	.personal-box {
		width: 92%;
		margin-left: auto;
		margin-right: auto;
		padding-bottom: 80rpx;
	}

	.idcar {
		border-radius: 8rpx;
	}

	.title-box {
		text-align: center;
		font-size: 32rpx;
		padding: 10rpx 0 40rpx 0;
	}

	.card_box {
		padding: 10rpx 30rpx;
	}

	.card_text_login {
		background-color: #014AF8;
		color: #FFFFFF;
		font-size: 30rpx;
		text-align: center;
		padding-top: 22rpx;
		padding-bottom: 22rpx;
		margin-top: 84rpx;
		border-radius: 8rpx;
	}
	td {
		font-size: 12rpx;
	}
</style>